<template>
  <div class="order-container">
    <!-- 头部面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card class="margin-top">
      <el-row>
        <el-col :span="8">
          <!-- 输入框 -->
          <el-input placeholder="请输入内容">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="goodsList">
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column label="订单价格" prop="order_price"></el-table-column>
        <el-table-column label="是否付款">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.order_pay === '1'"
              >已付款</el-tag
            >
            <el-tag v-else type="danger">未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send"></el-table-column>
        <el-table-column label="下单时间">
          <template slot-scope="scope">
            {{ scope.row.create_time | dateFormate }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="showUpdataDialog"
            ></el-button>
            <el-button
              size="mini"
              type="success"
              icon="el-icon-location"
              @click="showProgress"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        class="margin-top"
      >
      </el-pagination>
    </el-card>

    <!-- 修改地址对话框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="updateDialogVisible"
      width="50%"
      @close="addressDialogClosed"
    >
      <el-form
        :model="addressForm"
        :rules="addressFormRules"
        ref="addressFormRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            v-model="addressForm.address1"
            :options="cityData"
            :props="cascaderProps"
            @change="handleChange"
            style="width: 100%"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="updateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateDialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 物流进度对话框 -->
    <el-dialog title="提示" :visible.sync="showProgressVisible" width="50%">
      <span>这是一段信息</span>
    </el-dialog>
  </div>
</template>

<script>
// 导入getOrderListAPI接口
import { getOrderListAPI } from '@/api/getOrderListAPI.js'

// 导入中国省市
import cityData from '@/views/order/city_data2017_element.js'

// 导入getAdressAPI接口
// import { getAdressAPI } from '@/api/getAdressAPI.js'
export default {
  name: 'Order',
  data() {
    return {
      queryInfo: {
        query: '',
        // 当前所在页
        pagenum: 1,
        // 页面显示的数据条数
        pagesize: 5
      },
      // 订单的总数量
      total: 0,
      // 从后台获取过来的商品订单数量
      goodsList: [],
      // 控制修改地址对话框显示隐藏
      updateDialogVisible: false,
      // 省市区县的列表
      cityData,
      // 修改地址提交的表单数据
      addressForm: {
        address1: [],
        address2: ''
      },
      // 修改地址表单与验证
      addressFormRules: {
        address1: [
          { required: true, message: '请选择省市区县', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请填写详细地址', trigger: 'blur' }
        ]
      },
      // 级联选择器的配置项
      cascaderProps: {
        expandTrigger: 'hover',
        checkStrictly: true,
        label: 'label',
        value: 'value'
      },
      // 控制物流进度对话框显示隐藏
      showProgressVisible: false
    }
  },
  methods: {
    async getOrderList() {
      const { data: res } = await getOrderListAPI(this.queryInfo)
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单列表失败')
      }
      this.total = res.data.total
      this.goodsList = res.data.goods
      this.$message.success('获取订单列表成功')
    },
    // 当数据条数发生变化会触发该事件处理函数
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    // 当页面的页数发生变化会触发改时间处理函数
    handleCurrentChange(newNum) {
      this.queryInfo.pagenum = newNum
      this.getOrderList()
    },
    // 点击编辑按钮显示修改地址对话框
    showUpdataDialog() {
      this.updateDialogVisible = true
    },
    // 级联下拉菜单值发生变化会触发该事件
    handleChange() {},
    // 当修改地址对话框关闭会触发改事件
    addressDialogClosed() {
      this.$refs.addressFormRef.resetFields()
    },
    // 点击显示按钮显示物流进度对话框
    showProgress() {
      // const { data: res } = await getAdressAPI()
      // if (res.meta.status !== 200) {
      //   return this.$message.error('获取物流进度失败')
      // }
      this.showProgressVisible = true
      // console.log(res)
    }
  },
  // vue生命周期函数，created是props、data、methods创建完毕阶段
  // 最早可以对数据、方法进行操作的时候
  created() {
    this.getOrderList()
  }
}
</script>

<style lang="less" scoped>
.margin-top {
  margin-top: 15px;
}
</style>
